package com.android.powerbankpad.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.android.powerbankpad.R;


/**
 * @创建者 ws
 * @创建时间 2021/11/2
 * @描述
 */
public class AlarmDialog extends Dialog {
    public View dg_divider_view;
    private Context mContext;
    private TextView mTitle;
    private TextView mContent;
    private TextView mRightBtn;
    private TextView mLeftBtn;

    private ImageView mDialogIcon;
    private LinearLayout dlg_2btn_layout;
    private OnDiaogClickListener mListener;
    private TextView bottom_line;

    public AlarmDialog(@NonNull Context context) {
        super(context, R.style.dialog_alarm);
        mContext = context.getApplicationContext();
        initUI();
    }

    public void setTitle(String titleStr) {
        if (titleStr != null) {
            mTitle.setText(titleStr);
        } else {
            mTitle.setText("提示");
        }
    }

    public void setContent(String titleStr) {
        if (titleStr != null) {
            mContent.setText(titleStr);
        }
    }

    public void setContent(int resId) {
        if (resId >= 0) {
            mContent.setText(resId);
        }
    }

    public void setContentLineSpacing(int lineSpace) {
        mContent.setLineSpacing(lineSpace, 1);
    }

    public void setContent(SpannableString text) {
        if (text != null) {
            mContent.setText(text);
        }
    }

    public void setSpanContent(SpannableString titleStr) {
        if (titleStr != null) {
            mContent.setText(titleStr);
        }
    }

    public void setLeftBtnStr(String lStr) {
        if (lStr != null) {
            mLeftBtn.setText(lStr);
        }
    }

    public void setRightBtnStr(String rStr) {
        if (rStr != null) {
            mRightBtn.setText(rStr);
        }
    }

    public void setGravity(int rStr) {
        mContent.setGravity(rStr);
    }

    public void setRightBtnBackground(Drawable drawable) {
        if (drawable != null) {
            mRightBtn.setBackgroundDrawable(drawable);
        }
    }

    public void setRightBtnBackground(int resid) {
        mRightBtn.setBackgroundResource(resid);
    }

    public void setRightBtnTextColor(int color) {
        mRightBtn.setTextColor(color);
    }

    public void setLeftBtnTextColor(int color) {
        mLeftBtn.setTextColor(color);
    }

    public void setLeftBtnListener(OnClickListener lListener) {
        mLeftBtn.setTag(lListener);
        mLeftBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                OnClickListener lListener = (OnClickListener) mLeftBtn.getTag();
                lListener.onClick(AlarmDialog.this, 0);
            }
        });
    }

    public void setRightBtnListener(OnClickListener rListener) {
        mRightBtn.setTag(rListener);
        mRightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                OnClickListener lListener = (OnClickListener) mRightBtn.getTag();
                lListener.onClick(AlarmDialog.this, 1);
            }
        });
    }

    private void initUI() {
        View dlgView = LayoutInflater.from(mContext).inflate(R.layout.dialog_alarm, null);

        mTitle = dlgView.findViewById(R.id.diaTitle);
        mContent = dlgView.findViewById(R.id.diaContent);
        dg_divider_view = dlgView.findViewById(R.id.diaDividerView);
        mLeftBtn = dlgView.findViewById(R.id.diaLeftBtn);
        mRightBtn = dlgView.findViewById(R.id.diaRighBtn);
        mDialogIcon = dlgView.findViewById(R.id.diaIcon);
        dlg_2btn_layout = dlgView.findViewById(R.id.dlg_2btn_layout);
        bottom_line = dlgView.findViewById(R.id.bottomLine);
        OnClickListener listener = new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int arg1) {
                if (mListener != null) {
                    mListener.onClick(arg1);
                }
                dialog.dismiss();
            }
        };
        setLeftBtnListener(listener);
        setRightBtnListener(listener);
        setContentView(dlgView);
        //setCanceledOnTouchOutside(true);


    }

    public void setDialogIcon(int id) {
        mDialogIcon.setImageResource(id);
    }

    public void setDialogIcon(Bitmap map) {
        mDialogIcon.setImageBitmap(map);
    }

    public void setContentGravity(int gravity) {
        mContent.setGravity(gravity);
    }

    public void setSureButtonText(String mText) {
        if (mText != null) {
            mRightBtn.setText(mText);
        }
    }

    public void setTitleVisibility(int flag) {
        mTitle.setVisibility(flag);
    }

    // public void setOnClickListener(OnDiaogClickListener listener) {
    //        mListener = listener;
    //    }

    public void setLeftBtnVisibility(boolean flag) {
        if (!flag) {
            mLeftBtn.setVisibility(View.GONE);
        }
    }

    public void setRightBtnVisibility(boolean flag) {
        if (!flag) {
            mRightBtn.setVisibility(View.GONE);
            findViewById(R.id.diaDividerView).setVisibility(View.GONE);
        }
    }

    public void setRightBtnParam(float width, float height) {
        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) mRightBtn.getLayoutParams();
        linearParams.height = (int) height;
        linearParams.width = (int) width;
        mRightBtn.setLayoutParams(linearParams);
    }

    public void setDialogIconVisibility(boolean flag) {
        if (flag) {
            mDialogIcon.setVisibility(View.VISIBLE);
        } else {
            mDialogIcon.setVisibility(View.GONE);
        }
    }

    public void setDialog2btn_layoutVisibility(boolean flag) {
        if (flag) {
            dlg_2btn_layout.setVisibility(View.VISIBLE);
        } else {
            dlg_2btn_layout.setVisibility(View.GONE);
        }
    }

    public void setDialogmTitleVisibility(boolean flag) {
        if (flag) {
            mTitle.setVisibility(View.VISIBLE);
        } else {
            mTitle.setVisibility(View.GONE);
        }
    }

    public void setDialogbottom_lineisibility(boolean flag) {
        if (flag) {
            bottom_line.setVisibility(View.VISIBLE);
        } else {
            bottom_line.setVisibility(View.GONE);
        }
    }

    public void setCanceledOnTouch(boolean isCanceledOnTouch) {

        setCanceledOnTouchOutside(isCanceledOnTouch);
    }


    public interface OnDiaogClickListener {
        void onClick(int which);
    }
}
